package com.neveu.android.ds.groupmessenger;

import java.util.Arrays;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class GroupMessengerActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
	
	private Messenger mes;
	private Resources res;
	private TextView tv;
	private TextView cpMsg_tv;
	private static Context appContext;
	private Uri providerUri;
	private Cursor msgCursor;
	
	public static Context getContext()
	{
		return appContext;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        res = this.getResources();
        tv = ((TextView)findViewById(R.id.textview));
        tv.setMovementMethod(new ScrollingMovementMethod());
        
        cpMsg_tv = ((TextView)findViewById(R.id.cpMsg_tv));
        
        //getText(R.string.loopback_ip)
	    Button sendButton = (Button)findViewById(R.id.send_but);
	    
	    appContext = this.getApplicationContext();
	    mes = new Messenger(this);
		providerUri = Uri.parse("content://" + res.getString(R.string.PROVIDER_URI));

	    sendButton.setOnClickListener(this);

	    Spinner spinner = (Spinner) findViewById(R.id.cpMsgNum);
	    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, Arrays.asList(0,1,2,3,4,5));
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);

	    spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
	    	public void onItemSelected(AdapterView<?> parent,
	    	        View view, int pos, long id) {
	    		
	    		if(pos == 0)
	    		{
	    			cpMsg_tv.setText("--");
	    			return;
	    		}
	    		
    			msgCursor = appContext.getContentResolver().query(providerUri, null, String.valueOf(pos), null, null);
    			msgCursor.moveToFirst();
    			cpMsg_tv.setText(msgCursor.getString(1));
	    	}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
	    });
    }
  
    
    public void onClick(View v)
    {
    	switch(v.getId())
    	{
    	case R.id.send_but:
			EditText et = (EditText)findViewById(R.id.editText1);
			mes.send(et.getText().toString());
			et.setText("");
    		break;
    	}
    }

    @Override
    public void onStop()
    {
    	super.onStop();
    	mes.finish();
    	this.finish();
    }
    
    public void receiveMessage(final String msg)
    {
    	tv.post(new Runnable() {
            public void run() {
            	tv.append(msg + "\n");

            	if(tv.getLineCount() > 5 /* need to way to get max lines */)
            		tv.scrollBy(0, tv.getLineHeight());
            }
    	});
    }

}